package com.exm.collectcodenew.web.controller;

import com.exm.collectcodenew.web.entity.UserEnumStatus;
import com.exm.collectcodenew.web.service.UserEnumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserEnumController {
    @Autowired
    private UserEnumService userEnumService;
    /**
     * 获取枚举类的所有值
     */
    @GetMapping("/status/list")
    public List<UserEnumStatus> getUserEnumStatusList() {
        return userEnumService.getAllEnums(UserEnumStatus.class);
    }
    /**
     * 获取枚举的code-desc映射
     */
    @GetMapping("/status/listMap")
    public Map<Object, String> getUserEnumStatusListMap() {
        return userEnumService.getCodeDescMap(UserEnumStatus.class);
    }

    /**
     * 根据枚举类名和code查询枚举
     */
    @PostMapping("/query-by-code")
    public Object queryEnumByCode(@RequestBody Map<String, Object> request) {
        try {
            String className = (String) request.get("className");
            Object code = request.get("code");

            Class<?> enumClass = Class.forName(className);
            if (enumClass.isEnum()) {
                return userEnumService.getEnumByCode((Class<Enum>) enumClass, code);
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException("查询枚举失败", e);
        }
    }
    /**
     * 根据枚举类名的名称查询
     */
    @GetMapping("/query-by-name")
    public Object queryEnumByName(@RequestParam String className,
                                  @RequestParam String name) {
        try {
            Class<?> enumClass = Class.forName(className);
            if (enumClass.isEnum()) {
                return userEnumService.getEnumByName((Class<Enum>) enumClass, name);
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException("查询枚举失败", e);
        }
    }



    @GetMapping("/status/{code}")
    public UserEnumStatus getUserEnumStatusByCode(@PathVariable Integer code) {
        return userEnumService.getEnumByCode(UserEnumStatus.class, code);
    }

    @GetMapping("/status/active")
    public List<UserEnumStatus> getActiveStatus() {
        return userEnumService.getEnumsByCondition(UserEnumStatus.class,
                status -> status.getCode() > 0);
    }

}
